/* eslint-disable react/jsx-handler-names */
/* eslint-disable eqeqeq */
/** 
 * 入参
    type： 1- 指令   2-簿记
    onOk：指令下达成功或簿记成功抛出事件
    onCancel：取消抛出事件

*/
import React, { PureComponent } from 'react';
import { AvailableAssetC, TradeHoldC, FirstHeaderC } from '../../../../components';
import style from './index.less';
import BaseInfoC from './baseInfoC';
import AddOrderC from './addOrder';
import 'moment/locale/zh-cn';
import { connect } from 'dva';

@connect(({ nonstandarmaintenanceNewM, globalM }) => ({ nonstandarmaintenanceNewM, globalM }))
export default class AddNonstandardOrder extends PureComponent {
  static defaultProps = {
    onOk: () => {},
    onCancel: () => {},
  };

  constructor(props) {
    super(props);
    this.state = {
      timeStamp: new Date().getTime(), // 时间戳
      money: {}, // 可用金额，
      position: {}, // 可用数量
      portfolio: undefined, // 当前组合
      tradingcode: undefined, // 当前证券代码 {exchangecode: "101", tradingcode: "510060", secuabbr: "上证中央企业50ETF"}
      productGroupId: undefined, // 产品组id
      info: undefined, // 基础信息
      portfolioCode: undefined, // 组合code
    };
  }

  componentDidMount() {
    const {
      globalM: { dataDictionary = {} },
    } = this.props;
    this.props.dispatch({
      type: 'nonstandarmaintenanceNewM/fetchDictByKeys',
      payload: {
        dictData: dataDictionary,
      },
    });
  }

  // 组合变更id 切换
  portfolioOnChange = (e) => {
    if (this.state.portfolio !== e) {
      // 组合id不同则变更当前页面组合id
      this.setState({
        portfolio: e,
      });
    }
  };

  // 产品组合变更id 切换
  productGroupOnChange = (e) => {
    if (this.state.productGroupId !== e) {
      // 产品组合id不同则变更当前页面组合id
      this.setState({
        productGroupId: e,
      });
    }
  };

  // 证券代码变更
  tradingcodeOnChange = (e) => {
    if (this.state.tradingcode !== e) {
      // 证券代码不同则变更当前页面证券代码
      this.setState({
        tradingcode: e,
      });
    }
    if (!e) {
      this.setState({
        info: undefined,
      });
    }
  };

  // 获取组合code
  onChangePortfolioCode = (val) => {
    this.setState({
      portfolioCode: val, // 组合代码
    });
  };

  // 更新时间戳 用于更新资金可用或持仓可用
  handleChangeTimeStamp = (type) => {
    // eslint-disable-next-line no-undef
    this.setState({ [type]: new Date().getTime() });
  };

  // 新建指令和簿记成功之后调用
  onOk = () => {
    const { type } = this.props;
    if (type == 2 || type == 11 || type == 12) {
      // 簿记页面
      this.props.onOk();
    }
  };

  // 点击取消按钮
  onCancel = () => {
    const { type } = this.props;
    if (type == 2 || type == 11 || type == 12) {
      // 簿记页面
      this.props.onCancel();
    }
    if (Number(type) === 2) {
      // 需要清除code
      this.setState({
        portfolioCode: undefined,
      });
    }
  };

  render() {
    const { portfolio, tradingcode, productGroupId, portfolioCode, timeStamp1, timeStamp2 } = this.state;
    const { type } = this.props;
    return (
      <div className={style.addNonstandardOrder}>
        <div>
          <div>
            <div>
              <BaseInfoC tradingCode={tradingcode && tradingcode.tradingCode ? tradingcode.tradingCode : undefined} />
            </div>
          </div>
          <div>
            <div style={{ marginBottom: '14px' }}>
              <AvailableAssetC
                timeStamp={timeStamp1}
                queryModel={'new'}
                portfolio={portfolioCode}
                type={this.props.type}
                productGroupId={productGroupId}
                onChange={(e) => {
                  this.setState({
                    money: e,
                  });
                }}
              />
            </div>
            <div style={{ marginBottom: '20px' }}>
              <TradeHoldC
                securityType={'12'} // 非标
                type={this.props.type}
                portfolio={portfolioCode}
                timeStamp={timeStamp2}
                queryModel={'new'}
                productGroupId={productGroupId}
                tradingcode={tradingcode && tradingcode.tradingCode ? tradingcode.tradingCode : undefined}
                exchangecode={tradingcode && tradingcode.exchangeCode ? tradingcode.exchangeCode : undefined}
                onChange={(e) => {
                  this.setState({
                    position: { ...e },
                  });
                }}
              />
            </div>
            <FirstHeaderC title={'交易要素'} />
            <AddOrderC
              type={this.props.type}
              portfolioOnChange={this.portfolioOnChange}
              tradingcodeOnChange={this.tradingcodeOnChange}
              productGroupOnChange={this.productGroupOnChange}
              onChangePortfolioCode={this.onChangePortfolioCode}
              changeTimeStamp={this.handleChangeTimeStamp}
              onRef={(ref) => {
                this.addOrder = ref;
              }}
              onOk={this.onOk}
              onCancel={this.onCancel}
              money={this.state.money}
              position={this.state.position}
            />
          </div>
        </div>
      </div>
    );
  }
}
